//获得项目地址
let allpath=window.location.href;
let pathname=window.location.pathname;
let index = allpath.indexOf(pathname,0);
let pathIndex = pathname.indexOf("/",1);
let path=allpath.substring(0,index)+pathname.substring(0,pathIndex)
//加载用户数据
$(function () {
    $.ajax({
        url: path + "/admin/getUser",
        type: "GET",
        success: function (data) {
            for (let i = 0; i < data.length; i++) {
                $("#tbody").append("<tr>\n" +
                    "    <td>\n" +
                    "        <label class='lyear-checkbox checkbox-primary'>\n" +
                    "            <input type='checkbox' name='ids[]' value='" + data[i].id + "'><span></span>\n" +
                    "        </label>\n" +
                    "    </td>\n" +
                    "    <td>" + data[i].id + "</td>\n" +
                    "    <td>" + data[i].userName + "</td>\n" +
                    "    <td>" + data[i].userPassword + "</td>\n" +
                    "    <td>" + data[i].userRole + "</td>\n" +
                    "    <td>" + formatDate(data[i].createDate) + "</td>\n" +
                    "    <td><span class='text-success'>" + data[i].userAccount + "</span></td>\n" +
                    "    <td>\n" +
                    "        <div class='btn-group'>\n" +
                    "            <a class='btn btn-xs btn-default'  onclick='alter_data(" + data[i].id + ")' title='编辑' data-toggle='tooltip'><i\n" +
                    "                class=\"mdi mdi-pencil\"></i></a>\n" +
                    "            <button class='btn btn-xs btn-default'  title='删除' onclick='deleteUser(" + data[i].id + ")' data-toggle='tooltip'><i\n" +
                    "                class='mdi mdi-window-close'></i></button>\n" +
                    "        </div>\n" +
                    "    </td>\n" +
                    "</tr>")
            }
        }
    })

})
//查询用户
function QueryUserByName() {
    $.ajax({
        url: path + "/admin/getUser",
        type: "GET",
        data: {"userName": $("#keyword").val()},
        success: function (data) {
            $("#tbody").html("");
            for (let i = 0; i < data.length; i++) {
                $("#tbody").append("<tr>\n" +
                    "    <td>\n" +
                    "        <label class='lyear-checkbox checkbox-primary'>\n" +
                    "            <input type='checkbox' name='ids[]' value='" + data[i].id + "'><span></span>\n" +
                    "        </label>\n" +
                    "    </td>\n" +
                    "    <td>" + data[i].id + "</td>\n" +
                    "    <td>" + data[i].userName + "</td>\n" +
                    "    <td>" + data[i].userPassword + "</td>\n" +
                    "    <td>" + data[i].userRole + "</td>\n" +
                    "    <td>" + formatDate(data[i].createDate) + "</td>\n" +
                    "    <td><span class='text-success'>" + data[i].userAccount + "</span></td>\n" +
                    "    <td>\n" +
                    "        <div class='btn-group'>\n" +
                    "            <a class='btn btn-xs btn-default'  title='编辑'  data-toggle='modal' onclick='alter_data(" + data[i].id + ")'><i\n" +
                    "                class='mdi mdi-pencil'></i></a>\n" +
                    "            <button class='btn btn-xs btn-default'  title='删除' onclick='deleteUser(" + data[i].id + ")' data-toggle='tooltip'><i\n" +
                    "                class='mdi mdi-window-close'></i></button>\n" +
                    "        </div>\n" +
                    "    </td>\n" +
                    "</tr>")
            }
        }
    })
}
//删除一个账户
function deleteUser(data) {
    Swal.fire({
        title: '确认删除用户?',
        text: "你在尝试删除一个用户!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes'
    }).then((result) => {
        if (result.value) {
            $.ajax({
                url: path + "/admin/deleteUser",
                type: "GET",
                data: {"id": data},
                success: function (data) {
                    if (data === 0) {
                    } else {
                        Swal.fire({
                            icon: 'success',
                            title: '删除成功',
                            allowOutsideClick: false,
                        }).then($(".swal2-actions button").click(function () {
                            history.go();
                        }))
                    }
                }
            })
        }
    })

}


//时间戳转换方法    date:时间戳数字
function formatDate(date) {
    var date = new Date(date);
    var YY = date.getFullYear() + '-';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return YY + MM + DD + " " + hh + mm + ss;
}

//消除上次的记录
let AdminUserName;
AdminUserName = {status: "false"}
let AdminUserAccount;
AdminUserAccount = {status: "false"}
let AdminUserPassword;
AdminUserPassword = {status: "false"}

//对数据的检验
function admin_checkUserName() {
    $.ajax({
        url: path + "/register/username",
        type: "GET",
        data: {"userName": $("#userName").val()},
        success: function (data) {
            if (data === "false") {
                $("#userName").addClass("is-valid");
                $("#userName").removeClass("is-invalid");
                AdminUserName.status = "true"
            } else if (data === "error") {
                $("#userName").addClass("is-invalid")
                $("#userName").removeClass("is-valid")
            } else {
                $("#userName").addClass("is-invalid")
                $("#userName").removeClass("is-valid")
            }
        }
    })
}
//检查用户的账户唯一
function admin_checkUseraccount() {
    $.ajax({
        url: path + "/register/userAccount",
        type: "GET",
        data: {"userAccount": $("#userAccount").val()},
        success: function (data) {
            if (data === "false") {
                $("#userAccount").addClass("is-valid");
                $("#userAccount").removeClass("is-invalid");
                AdminUserAccount.status = "true"
            } else if (data === "error") {
                $("#userAccount").addClass("is-invalid")
                $("#userAccount").removeClass("is-valid")
            } else {
                $("#userAccount").addClass("is-invalid")
                $("#userAccount").removeClass("is-valid")
            }
        }
    })
}
//检查用户密码
function admin_checkUserPassword() {
    let password = $("#userPassword").val();
    if (password.length < 20 && password.length > 6) {
        $("#userPassword").addClass("is-valid");
        $("#userPassword").removeClass("is-invalid");
        AdminUserPassword.status = "true";
    } else {
        $("#userPassword").addClass("is-invalid")
        $("#userPassword").removeClass("is-valid")
    }
}

//提交修改
function commit_result() {
    if (AdminUserName.status === "true" && AdminUserAccount.status === "true" && AdminUserPassword.status === "true") {

        $.ajax({
            url: path + "/admin/addUser",
            type: "GET",
            data: {
                "userName": $("#userName").val(),
                "userPassword": $("#userPassword").val(),
                "userAccount": $("#userAccount").val()
            },
            success: function (data) {
                if (data === true) {
                    Swal.fire({
                        icon: 'error',
                        title: '添加失败，请重试',
                        allowOutsideClick: false,
                    }).then($(".swal2-actions button").click(function () {
                        history.go();
                    }))
                } else {
                    Swal.fire({
                        icon: 'success',
                        title: '添加成功',
                        allowOutsideClick: false,
                    }).then($(".swal2-actions button").click(function () {
                        history.go();
                    }))
                }
            }
        })
    } else {
        Swal.fire({
            icon: 'warning',
            title: '再确定一下信息',
            allowOutsideClick: false,
        })
    }
}


//对选中的数据进行一个操作

function admin_checked() {
    Swal.fire({
        title: '确认删除用户?',
        text: "你在尝试删除用户!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes'
    }).then((result) => {
        if (result.value) {
            let box = new Array();
            let checkboxes = $("#tbody input[type=checkbox]:checked");
            box.push("First");
            for (let i = 0; i < checkboxes.length; i++) {
                box.push(checkboxes[i].value)
            }
            box.push("final")
            let s = JSON.stringify(box);
            $.ajax({
                url: path + "/admin/deleteUserByIds",
                type: "GET",
                dataType: "json",
                data: {"checked": s},
                success: function (data) {
                    if (data === true) {
                        Swal.fire({
                            icon: 'success',
                            title: '删除成功',
                            allowOutsideClick: false,
                        }).then($(".swal2-actions button").click(function () {
                            history.go();
                        }))
                    } else {
                        Swal.fire({
                            icon: 'error',
                            title: '删除失败',
                            allowOutsideClick: false,
                        }).then($(".swal2-actions button").click(function () {
                            history.go();
                        }))
                    }
                }
            })
        }
    })
}


//修改用户数据
function alter_data(data) {
    $.ajax({
        url: path + "/admin/getUserById",
        type: "GET",
        data: {"id": data},
        success: function (data) {
            Swal.fire({
                width: '700px',
                html: "<form>\n" +
                    "  <div class='form-row'>\n" +
                    "<input id='id' type='hidden' value='" + data.id + "'>" +
                    "    <div class='form-group col-md-3'>\n" +
                    "      <label for='admin_userName'>用户姓名</label>\n" +
                    "      <input type='text' class='form-control' id='admin_userName'  value='" + data.userName + "'>\n" +
                    "    </div>\n" +
                    "    <div class='form-group col-md-3'>\n" +
                    "      <label for='admin_userPassword'>用户密码</label>\n" +
                    "      <input type='text' class='form-control' id='admin_userPassword'  value='" + data.userPassword + "'>\n" +
                    "    </div>\n" +
                    "    <div class='form-group col-md-3'>\n" +
                    "      <label for='admin_userAccount'>用户账户</label>\n" +
                    "      <input type='text' class='form-control' id='admin_userAccount'  value='" + data.userAccount + "'>\n" +
                    "    </div>\n" +
                    "    <div class='form-group col-md-3'>\n" +
                    "      <label for='admin_inputState'>用户权限</label>\n" +
                    "      <select id='admin_inputState' class='form-control' >\n" +
                    "        <option value='0' selected>请选择------></option>\n" +
                    "        <option value='1'>系统管理员(1)</option>\n" +
                    "        <option value='2'>游客(2)</option>\n" +
                    "      </select>\n" +
                    "    </div>\n" +
                    "  </div>\n" +
                    "</form>"
            }).then((result) => {
                let admin_userPassword = document.getElementById("admin_userPassword");
                let admin_userName = document.getElementById("admin_userName");
                let admin_userAccount = document.getElementById("admin_userAccount");
                let id = document.getElementById("id");
                let userRole = document.getElementById("admin_inputState");
                if (result.isConfirmed) {
                    if ($("#inputState").val() == 0) {
                        Swal.fire({
                            icon: 'error',
                            title: '角色选择错误',
                            allowOutsideClick: false,
                        })
                    } else {
                        Swal.fire({
                            icon: 'question',
                            title: '确认修改吗',
                            allowOutsideClick: false,
                        }).then((result) => {

                                if (result.isConfirmed) {
                                    $.ajax({
                                        url: path + "/admin/updateUserInfo",
                                        type: "GET",
                                        data: {
                                            "id": id.value,
                                            "userName": admin_userName.value,
                                            "userPassword": admin_userPassword.value,
                                            "userAccount": admin_userAccount.value,
                                            "inputState": userRole.value
                                        },
                                        success: function (data) {
                                            if (data != 0) {
                                                Swal.fire({
                                                    icon: 'success',
                                                    title: '更新成功',
                                                    allowOutsideClick: false,
                                                }).then($(".swal2-actions button").click(function () {
                                                    history.go();
                                                }))
                                            } else {
                                                Swal.fire({
                                                    icon: 'error',
                                                    title: '更新失败',
                                                    allowOutsideClick: false,
                                                }).then($(".swal2-actions button").click(function () {
                                                    history.go();
                                                }))
                                            }
                                        }
                                    })
                                }
                            }
                        )
                    }
                }


            })
        }
    })
}

